//
//  XYHomeCell.swift
//  WeiboSourceCode
//
//  Created by 周星宇 on 15/12/31.
//  Copyright © 2015年 cn.weibo. All rights reserved.
//

import UIKit
import SnapKit
class XYHomeCell: UITableViewCell {
    
    private var toolBarTopConstraint: Constraint?
    var statusViewModel: XYStatusViewModel? {
        didSet{
            originalView.statusViewModel = statusViewModel
            homeToolBar.statusViewModel = statusViewModel
            // 判断是否有转发微博，如果有，就显示转发微博的视图
            // 如果没有，隐藏转发微博的视图
            // 在更新约束之前，把之前添加好的约束给 uninstall
            
            self.toolBarTopConstraint?.uninstall()
            if statusViewModel?.status?.retweeted_status != nil {
                                retweetedView.hidden = false
                retweetedView.statusViewModel = statusViewModel
                homeToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.toolBarTopConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
            }else{
                
                retweetedView.hidden = true
                homeToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpCell()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpCell() {
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(homeToolBar)
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.width.equalTo(contentView)
        }
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.equalTo(contentView)
            make.width.equalTo(contentView)
        }
        homeToolBar.snp_makeConstraints { (make) -> Void in
            
            
            toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(35)
        }
//         非常重要
        contentView.snp_makeConstraints { (make) -> Void in
            // 决定 cell 的高度
            make.bottom.equalTo(homeToolBar.snp_bottom)
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
    // 懒加载控件
    private lazy var originalView: XYOriginalStatusView = XYOriginalStatusView()
    private lazy var retweetedView: XYRetweetedView = XYRetweetedView()
    private lazy var homeToolBar: XYHomeToolBar = XYHomeToolBar()
    
}
